package com.holdtime.jtt809dataservice.server.handler;

import com.holdtime.jtt809dataservice.common.NettyCommon;
import com.holdtime.jtt809dataservice.common.domain.ChannelHandlerContextManager;
import com.holdtime.jtt809dataservice.common.domain.SubordinatePlatformThreadPoolManager;
import com.holdtime.jtt809dataservice.server.runnable.DealSubordinatePlatformMessageReceive;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * author: sunwei
 * Date: 2025/3/14 17:48
 * Description:
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class JTT809MainLinkServerHandler extends ChannelInboundHandlerAdapter {

    @Resource
    private ApplicationContext context;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        SubordinatePlatformThreadPoolManager poolManager = SubordinatePlatformThreadPoolManager
                .getCurrent();
        ThreadPoolExecutor executor = poolManager.getThreadPool();
        executor.execute(new DealSubordinatePlatformMessageReceive(ctx, msg,context));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelInactive(ctx);
        /**
         * 下线时删除缓存中对应的client的缓存
         */
        Channel channel = ctx.channel();
        InetSocketAddress insocket = (InetSocketAddress) channel
                .remoteAddress();
        Attribute<String> attr = channel.attr(NettyCommon.numberKey);
        if (!channel.isActive()) {
            log.debug("JTT809-Server断开" + insocket.toString() + "=>" + attr.get());
            if (attr != null && attr.get() != null) {
                ChannelHandlerContext channelHandlerContext = ChannelHandlerContextManager
                        .getCurrent().get(attr.get());
                if (channelHandlerContext == ctx) {
                    ChannelHandlerContextManager.getCurrent().remove(attr.get());
                }
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        Channel channel = ctx.channel();
        InetSocketAddress insocket = (InetSocketAddress) channel
                .remoteAddress();
        log.error("JTT809-Server连接异常:{}",insocket.toString());
        ctx.channel().close();
        //cause.printStackTrace();
    }

}
